// 下面开始逻辑
const id = window.localStorage.getItem("id");
const token = window.localStorage.getItem("token");
if (!id || !token) {
    window.location.href = `./login.html`;
}
// 判断登录状态是否到期
getLogin();
function getLogin() {
    ajax({
        url: "http://localhost:8888/users/info",
        data: `id=${id}`,
        token: token,
        dataType: "json",
        success(res) {
            if (res.code === 1) {
                console.log(res);
                $(".loginBox>li:first").text(`欢迎您,${res.info.nickname}`);
                $(".nickName").text(`${res.info.nickname}`);
                $(".shadowNick").text(`${res.info.nickname}`);
            } else {
                window.location.href = `./login.html`;
            }
        },
    });
}

// 渲染搜索下分类
getSort();
function getSort() {
    $.get("http://localhost:8888/goods/category", (res) => {
        let str = "";
        let str2 = "";
        res.list.slice(3, 14).forEach((item) => {
            $(".search>.top>p").html((str += `<span>${item}</span>`));
        });
        res.list.forEach((item) => {
            $(".listDis>p").html((str2 += `<span>${item}</span>`));
        });
    });
}

// 渲染列表页
getList();
function getList() {
    $.get("http://localhost:8888/goods/list", `pagesize=120`, res => {
        console.log(res);
        let str = "";
        [...res.list].slice(60, 120).forEach(item => {
            $(".render").html(
                (str += `<div class="goods" data-id='${item.goods_id}'>
        <div class="goodsPic">
            <img src="${item.img_big_logo}" alt="">
        </div>
        <p class="goodsTitle">${item.title}</p>
        <span class="goodsPrice">￥${item.current_price}</span>&nbsp;<span class="goodsPeo">${item.goods_number}人购买</span>
        </div>
            `)
            );
        });
    });
}

// 这是点击退出登录
outLogin();
function outLogin() {
    $(".outLogin").click(() => {
        window.localStorage.removeItem("id");
        window.localStorage.removeItem("token");
        window.location.reload();
    });
}

// 样式
getCss();
function getCss() {
    // 登录模块
    $(".ind").mouseenter(function () {
        $(".ind").css("background-color", "white");
        $(".loginShadow").css("display", "block");
    });

    $(".loginShadow").mouseleave(function () {
        $(".ind").css("background-color", "rgba(0,0,0,0)");
        $(".loginShadow").css("display", "none");
    });

    // 首页分类模块
    $(".lef").mouseenter(function () {
        $(".listDis").css("display", "block");
    });
    $(".lef").mouseleave(function () {
        $(".listDis").css("display", "none");
    });

    // nav显示隐藏 页面滚动事件
    window.onscroll = function () {
        var scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop > 100) {
            $("nav").slideDown(0.1);
        } else {
            $("nav").slideUp(0.1);
        }
    };

    //点击商品进入详情页
    $(".render").on("click", ".goods", e => {
        e = e || window.event;
        const target = e.target;
        console.log(target);
        window.localStorage.setItem("goods_id", `${target.dataset.id}`);
        window.location.href = "./detail.html";
    });
}

// 渲染模糊搜索
const search = document.querySelector('.top>input')
const searchBox = document.querySelector('.searchBox1')
search.oninput = function () {
    const text = this.value
    const script = document.createElement('script')
    // 淘宝搜索引擎
    script.src = 'https://suggest.taobao.com/sug?code=utf-8&q='+ text +'&_ksTS=1628752317697_496&callback=fn&k=1&area=c2c&bucketid=2'
    document.body.appendChild(script)
    script.remove()
}
function fn(res) {
    if (res.result.length === 0) {
        searchBox.style.display = 'none'
        return
    }

    let str = ''
    res.result.forEach(item => {
        str += `<li>${item[0]}</li>`
    })
    searchBox.innerHTML = str
    searchBox.style.display = 'block'
}


// 下面是swiper的样式
getSwiper();
function getSwiper() {
    var mySwiper = new Swiper(".swiper-container", {
        direction: "horizontal", // 横向切换选项
        loop: true, // 循环模式选项

        // 如果需要分页器
        pagination: {
            el: ".swiper-pagination",
        },
        autoplay: {
            delay: 1000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        },
        // 如果需要前进后退按钮
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
    });
}

// // 渲染模糊搜索
// const search = document.querySelector('.top>input')
// const searchBox = document.querySelector('.searchBox1')
// search.oninput = function () {
//     const text = this.value
//     const script = document.createElement('script')
//     // 百度搜索引擎
//     script.src = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=34099,33970,34004,33855,33607,26350,22159&wd=' + text + '&req=2&csor=5&pwd=aiqiyi&cb=fn&_=1623047595324'
//     document.body.appendChild(script)
//     script.remove()
// }
// function fn(res) {
//     if (!res.g) {
//         searchBox.style.display = 'none'
//         return
//     }

//     let str = ''
//     res.g.forEach(item => {
//         str += `<li>${item.q}</li>`
//     })
//     searchBox.innerHTML = str
//     searchBox.style.display = 'block'
// }

//楼层导航点击效果
$('.buildNav').on('click', '.one', () => {
    window.scrollTo({
        top: $('.list')[0].offsetTop - 60,
        behavior: 'smooth',
    })
})
$('.buildNav').on('click', '.five', () => {
    window.scrollTo({
        top: 0,
        behavior: 'smooth'
    })
})